package cn.wolfcode.shop.cloud.util;

import cn.wolfcode.shop.cloud.common.CommonControllerAdvice;
import cn.wolfcode.shop.cloud.common.Result;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;


@ControllerAdvice
public class MemberControllerAdvice extends CommonControllerAdvice {
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handlerBindException(BindException e){
        e.printStackTrace();
        String message = e.getAllErrors().get(0).getDefaultMessage();
        //public static final MemberCodeMsg PARM_OPT=new MemberCodeMsg(50103,"参数异常信息:{0}");
        MemberCodeMsg memberCodeMsg = MemberCodeMsg.PARM_OPT;
        //"参数异常信息:手机格式不正确"
        MemberCodeMsg memberCodeMsg1 = memberCodeMsg.format(message);
        return Result.error(memberCodeMsg1);
    }
}
